﻿using Dapper;
using OddUserManager.Data.IRepositories;
using OddUserManager.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OddUserManager.Data.Repositories
{
    public class JCTestStrategyGroupRepository : MicroDataDapperRepository<JCTestStrategyGroup, int>, IJCTestStrategyGroupRepository
    {
        public JCTestStrategyGroup Get(int id)
        {
            JCTestStrategyGroup model = null;
            using (var con = GetOpenConnection())
            {
                var sql = string.Format(@"select * from JCTestStrategyGroup where Id={0};
                select * from JCTestStrategyRelation where JCTestStrategyGroupId={1};
select j.* from JCTestStrategy j join JCTestStrategyRelation c on j.Id =c.JCTestStrategyId
where c.JCTestStrategyGroupId={2};", id, id, id);
                model = con.QueryMultiple(sql, null, null).Map<JCTestStrategyGroup, JCTestStrategyRelation, JCTestStrategy, int, int>
                      (
                      a => a.Id,
                      b => b.JCTestStrategyGroupId,
                      c => c.JCTestStrategyId,
                      d => d.Id,
                      (m, cs) => { m.JCTestStrategyRelationList = cs.ToList(); },
                      (c, ks) => { c.JCTestStrategy = ks.FirstOrDefault(); }
                      ).FirstOrDefault();
            }
            return model;
        }

        public List<JCTestStrategyGroup> GetAllJCTestStrategyGroups()
        {
            List<JCTestStrategyGroup> list = null;
            using (var con = GetOpenConnection())
            {
                var sql = @"select * from JCTestStrategyGroup;";
                //list = con.QueryMultiple(sql, null, null).Map<JCTestStrategyGroup, JCTestStrategyRelation, JCTestStrategy, int, int>
                //      (
                //      a => a.Id,
                //      b => b.JCTestStrategyGroupId,
                //      c => c.JCTestStrategyId,
                //      d => d.Id,
                //      (m, cs) => { m.JCTestStrategyRelationList = cs.ToList(); },
                //      (c, ks) => { c.JCTestStrategy = ks.FirstOrDefault(); }
                //      ).ToList();

                list = con.Query<JCTestStrategyGroup>(sql).ToList();
            }
            return list;
        }

        public IEnumerable<JCTestStrategyGroup> GetAllList()
        {
            List<JCTestStrategyGroup> list = null;
            using (var con = GetOpenConnection())
            {
                var sql = @"select * from JCTestStrategyGroup;";
                //list = con.QueryMultiple(sql, null, null).Map<JCTestStrategyGroup, JCTestStrategyRelation, JCTestStrategy, int, int>
                //      (
                //      a => a.Id,
                //      b => b.JCTestStrategyGroupId,
                //      c => c.JCTestStrategyId,
                //      d => d.Id,
                //      (m, cs) => { m.JCTestStrategyRelationList = cs.ToList(); },
                //      (c, ks) => { c.JCTestStrategy = ks.FirstOrDefault(); }
                //      ).ToList();

                list = con.Query<JCTestStrategyGroup>(sql).ToList();
            }
            return list;
        }

        public bool InsertOrUpdate(JCTestStrategyGroup entity)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<JCTestStrategyGroup> LoadPageList(int startPage, int pageSize, out int rowCount, Func<JCTestStrategyGroup, bool> where, Func<JCTestStrategyGroup, object> order)
        {
            throw new NotImplementedException();
        }
    }
}
